<template>
  <div>
    <!-- 面包导航 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/admin' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>图书管理</el-breadcrumb-item>
      <el-breadcrumb-item>图书列表</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 卡片视图区域 -->
    <el-card style="margin-left:-1rem;margin-top:1rem">
      <el-row :gutter="20">
        <el-col :span="8">
          <el-input placeholder="请输入内容" clearable @clear="getUserList">
            <el-button slot="append" icon="el-icon-search" @click="getUserList()"></el-button>
          </el-input>
        </el-col>
        <el-col :span="4">
          <el-button type="primary" @click="addVisibl()">添加图书</el-button>
        </el-col>
      </el-row>

      <!-- 图书列表区域 -->
      <el-table :data="userlist" border stripe style="margin-top:1rem">
        <el-table-column label="＃" type="index"></el-table-column>
        <el-table-column label="书名" prop="name"></el-table-column>
        <el-table-column label="作者" prop="auth"></el-table-column>
        <el-table-column label="分类" prop="title"></el-table-column>
        <el-table-column label="内容简介"  prop="textarea"></el-table-column>
     
        <el-table-column label="操作">
          <template slot-scope="scope"> 
            <!-- <el-button type="primary" icon="el-icon-edit" size="mini"></el-button> -->
            <!-- <el-button type="danger" @click="open" icon="el-icon-delete" size="mini"></el-button> -->
            <el-button type="success" size="mini" plain @click="addzj(scope.row.time)">添加章节</el-button>
            <el-button type="success" size="mini" plain @click="addzjlb(scope.row.time)">章节列表</el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>

    <!-- 添加图书对话框 -->
    <el-dialog
      title="添加图书"
      :visible.sync="addVisible"
      width="50%" @close="addDialogClosed">
      <!-- 主题区 -->
       <el-form  :model="addForm" :rules="addFormRules" ref="addFormRef" label-width="70px">
        <el-form-item prop="title" label="标题">
          <el-input v-model="addForm.title"></el-input>
        </el-form-item>
        <el-form-item prop="cid" label="所属分类">
         <el-select   v-model="value" placeholder="请选择">
          <el-option
            v-for="item in catelist"
            :key="item.id"
            :label="item.title"
            :value="item.id">
          </el-option>
        </el-select>
        </el-form-item>
        <el-form-item label="作者">
          <el-input v-model="addForm.auth"></el-input>
        </el-form-item>
         <el-form-item label="简介">
            <el-input
              type="textarea"
              :rows="2"
              placeholder="请输入内容"
              v-model="textarea">
              </el-input>
         </el-form-item>
         <!-- <div class="form-signin-heading" id="btnUpload" @change="upload">上传图片</div>
        <input type="file" name="avatar" id="avatar" multiple="multiple" @change="upload"> -->
        <!-- <img :src="'http://localhost:8888'+item.photos_url" alt=""/> -->
      </el-form>
      <!-- 底部区 -->
      <span slot="footer" class="dialog-footer">
        <el-button @click="addVisible = false">取 消</el-button>
        <el-button type="primary"  @click="onSubmit">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>    

<script>
import { setToken, setUserName } from "../../utils/auth";
import request from "../../utils/request";
export default {
  data(){
    return{
     
      userlist:[],
      catelist:{
        id:'',
        title:''
      },
      value: '',
      textarea:'',
      //控制添加图书对话框的显示喝隐藏
      addVisible:false,
        //添加图书的表单数据
      addForm:{
        title:'',
        cid:'',
        auth:'',
        textarea:''
     },
         //添加表单的验证规则对象
        addFormRules:{
          title:[
            { required: true, message: '请输入图书名称', trigger: 'blur' },
            { min: 3, max: 5, message: '长度在 3 到 5 个字符', trigger: 'blur' }
          ],
          auth:[
            { required: true, message: '请输入用户名', trigger: 'blur' },
            { min: 3, max: 12, message: '长度在 3 到 12 个字符', trigger: 'blur' }
          ]
        }
      }
    },


  created() {
    this.getUserList()
    // this.getUserCate()

  },
  methods: {
    //添加章节跳转
    addzj(id){
       this.$router.push({path:"/admin/books/add",query:{id:id}});
    },
    //章节列表
    addzjlb(id){
       this.$router.push({path:"/admin/books/addlst",query:{id:id}});
    },
     getUserList() {
      var that = this
     request({
        url: "/api/user/getAdminbooks",
        method: "post",
        data: {}
      }).then(res => {
        console.log(res)
       that.userlist = res.list;
        
      });
   
    },
    addVisibl(){
      this.addVisible = true
         //获取分类列表
        var that = this
      request({
          url: "/api/user/getAdminCate",
          method: "post",
          data: {}
        }).then(res => {
          console.log(res)
        that.catelist = res.list;
          
        });
    
     
    },
     //添加图书
    onSubmit() {
      var day = new Date();
      var timestamp = Date.parse(new Date());//获取当前时间
      const params = {
        name:this.addForm.title,
        cid:this.value,
        auth:this.addForm.auth,
        textarea:this.textarea,
        time:timestamp
     }
      request({
        url: "/api/user/addbooks",
        method: "post",
        data: params
      }).then(res => {
        if (res.msg === "新增成功") {
          this.$message({
            message: "恭喜你，新增成功",
            type: "success"
          });
          this.$router.push("/admin/welcome");
        }
      })
    },
    open() {
        this.$confirm('此操作将永久删除该图书, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.$message({
            type: 'success',
            message: '删除成功!'
          });
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          });          
        });
      }
  }
}
</script>

<style lang="less" scoped>
</style>